//
//  VCCheckPunch.m
//  MOSAD_HW2_1
//
//  Created by Jason on 2020/10/29.
//  Copyright © 2020 Jason. All rights reserved.
//

#import "VCCheckPunch.h"

@interface VCCheckPunch ()

@end

@implementation VCCheckPunch

@synthesize num = _num;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"查看打卡";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    
    //调用全局变量
    AppDelegate * myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    //定义并且创建一个滚动视图
    //可对视图内容进行滚屏查看功能
    UIScrollView * sv = [[UIScrollView alloc] init];
    sv.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    sv.scrollEnabled = YES;
    //设置画布大小，画布显示在滚动视图内部，一般大于Frame的大小
    sv.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
    //是否可以边缘弹动效果
    sv.bounces = YES;
    //设置背景颜色
    sv.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:sv];
    
    
    
    //计算time文本所需大小
    CGSize timeSize = [self getSize:myDelegate.time[_num]];
    UILabel * timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, self.view.bounds.size.width - 30, timeSize.height)];
    //多行显示，计算高度
    timeLabel.numberOfLines = 0;
    timeLabel.text = myDelegate.time[_num];
    timeLabel.font = [UIFont systemFontOfSize:18];
    timeLabel.textColor = [UIColor blackColor];
    timeLabel.backgroundColor = [UIColor whiteColor];
    [sv addSubview:timeLabel];
    
    //计算location文本所需大小
    CGSize locationSize = [self getSize:myDelegate.location[_num]];
    UILabel * locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20 + timeSize.height + 20, self.view.bounds.size.width - 30, locationSize.height)];
    //多行显示，计算高度
    locationLabel.numberOfLines = 0;
    locationLabel.text = myDelegate.location[_num];
    locationLabel.font = [UIFont systemFontOfSize:18];
    locationLabel.textColor = [UIColor blackColor];
    locationLabel.backgroundColor = [UIColor whiteColor];
    [sv addSubview:locationLabel];
    
    //计算scenicSpots文本所需大小
    CGSize scenicSpotsSize = [self getSize:myDelegate.scenicSpots[_num]];
    UILabel * scenicSpotsLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20 + timeSize.height + 20 + locationSize.height + 20, self.view.bounds.size.width - 30, scenicSpotsSize.height)];
    //多行显示，计算高度
    scenicSpotsLabel.numberOfLines = 0;
    scenicSpotsLabel.text = myDelegate.scenicSpots[_num];
    scenicSpotsLabel.font = [UIFont systemFontOfSize:18];
    scenicSpotsLabel.textColor = [UIColor blackColor];
    scenicSpotsLabel.backgroundColor = [UIColor whiteColor];
    [sv addSubview:scenicSpotsLabel];
    
    //计算experience文本所需大小
    CGSize experienceSize = [self getSize:myDelegate.experience[_num]];
    UILabel * experienceLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20 + timeSize.height + 20 + locationSize.height + 20 + scenicSpotsSize.height + 20, self.view.bounds.size.width - 30, experienceSize.height)];
    //多行显示，计算高度
    experienceLabel.numberOfLines = 0;
    experienceLabel.text = myDelegate.experience[_num];
    experienceLabel.font = [UIFont systemFontOfSize:17];
    experienceLabel.textColor = [UIColor blackColor];
    experienceLabel.backgroundColor = [UIColor whiteColor];
    [sv addSubview:experienceLabel];
    
    
    for (int i = 0; i < [myDelegate.pictureNums[_num] count]; i++) {
        UIImage * image = [UIImage imageNamed: [NSString stringWithFormat:@"%@.jpg", myDelegate.pictureNums[_num][i]]];
        UIImageView * iView = [[UIImageView alloc] initWithImage:image];
        iView.frame = CGRectMake(15 + (i%3)*(15+120), 20 + timeSize.height + 20 + locationSize.height + 20 + scenicSpotsSize.height + 20 + experienceSize.height + 20 + (i/3)*(120+15), 120, 120);
        iView.contentMode = UIViewContentModeScaleToFill;
        iView.clipsToBounds = YES;
        iView.backgroundColor = [UIColor whiteColor];
        
        //添加点击事件
        iView.userInteractionEnabled = YES;
        //添加手势
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressTap:)];
        //将手势添加到loginButton
        [iView addGestureRecognizer:tapGesture];
        //选择触发事件的方式（默认单击触发）
        [tapGesture setNumberOfTapsRequired:1];
        
        [sv addSubview:iView];

        if(sv.contentSize.height < (20 + timeSize.height + 20 + locationSize.height + 20 + scenicSpotsSize.height + 20 + experienceSize.height + 20 + (i%3)*(120+15) + 120) ){
            sv.contentSize = CGSizeMake(self.view.bounds.size.width, (20 + timeSize.height + 20 + locationSize.height + 20 + scenicSpotsSize.height + 20 + experienceSize.height + 20 + (i%3)*(120+15) + 120) + 50) ;
        }
    }
    
    
}

//计算文本大小以确定UILabel高度
-(CGSize) getSize : (NSString *)text {
        CGSize size = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil].size;
    return size;
}

-(void) pressTap:(UITapGestureRecognizer *) tap {
    UIImageView * imageView = (UIImageView *)tap.view;
    //创建显示视图控制器
    VCImageShow * imageShow = [[VCImageShow alloc] init];
    
    imageShow.image = imageView.image;
    //将控制器推出
    [self.navigationController pushViewController:imageShow animated:YES];
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
